ConsoleInput in; // tokenizer StringTokenizer tok; // line string line[0]; // our default model Word2Vec model; // loading any default here "glove-wiki-gigaword-50-tsne-2.txt" => string filepath; <<< "Mirrors and Blackholes..." >>>; model.load( filepath ); string current[3]; float vector[2]; fun int execute( string line[] ) { // check line[0] => string command; // index 1 => int index; if( command == "load" || command == "load" ) { if( line.size() < 2 ) { <<< "usage: load [model]", "" >>>; } else { <<< "loading model (this could take a few seconds)...", "" >>>; // load if( !model.load( me.dir() + filepath ) ) { <<< "cannot load model file...", "" >>>; return 1; } } } else if( command == "load-50" || command == "load-3" || command == "load-2" ) { string filepath; if( command == "load-50" ) "glove-wiki-gigaword-50.txt" => filepath; else if( command == "load-3" ) "glove-wiki-gigaword-50-pca-3.txt" => filepath; else "glove-wiki-gigaword-50-tsne-2.txt" => filepath; <<< "loading model:", filepath >>>; <<< "(this could take a few seconds)...", "" >>>; // load if( !model.load( me.dir() + filepath ) ) { <<< "cannot load model file...", "" >>>; return 1; } } else if( command == "eval" || command == "e" ) { if( line.size() < 2 ) { <<< "usage: eval [word expression]", "" >>>; <<< " for example: eval rome - italy + china", "" >>>; <<< " (note: don't forget white spaces around + and -)", "" >>>; } else { // expression string line[1] => string expr; // concatenate for( 2 => int i; i < line.size(); i++ ) { expr + " " + line[i] => expr; } // eval W2V.eval( model, expr, 3 ) @=> current; } } else if( command == "analog" || command == "a" ) { if( line.size() < 4 ) { <<< "usage: analog [wordA] [wordB] [wordC]", "" >>>; <<< " for example: analog cat kitten dog", "" >>>; } else { // expression string line[1] => string a; line[2] => string b; line[3] => string c; // eval W2V.analogy( model, a, b, c, 3 ) @=> current; // print } } else if( command == "vector" || command == "v" ) { if( line.size() < 2 ) { <<< "usage: vector [word]", "" >>>; } else { model.getVector( line[1], vector ); // print //cherr <= line[1] <= ": "; //for( int i; i < vector.size(); i++ ) { cherr <= vector[i] <= " "; } //cherr <= IO.newline(); } } return 0; // 0 is good } // word2vec helper class W2V { fun static void copy( float to[], float from[] ) { to.size(from.size()); // add into element for( int i; i < to.size(); i++ ) from[i] => to[i]; } fun static float[] dup( float from[] ) { float to[from.size()]; // add into element for( int i; i < to.size(); i++ ) from[i] => to[i]; // return return to; } // result stored in x fun static void add( float x[], float y[] ) { // smaller of the two x.size() < y.size() ? x.size() : y.size() => int size; // add into element for( int i; i < size; i++ ) y[i] +=> x[i]; } // result stored in x fun static void minus( float x[], float y[] ) { // smaller of the two x.size() < y.size() ? x.size() : y.size() => int size; // add into element for( int i; i < size; i++ ) y[i] -=> x[i]; } fun static void scale( float x[], float scalar ) { // scale for( int i; i < x.size(); i++ ) scalar *=> x[i]; } fun static void scale( float result[], float x[], float scalar ) { // scale for( int i; i < x.size(); i++ ) scalar * x[i] => result[i]; } fun static string[] eval( Word2Vec @ w, string expr, int k ) { // init int pos; 1.0 => float multiplier; string word; float wordVector[w.dim()]; float exprVector[w.dim()]; // compute while( true ) { expr.find(" ") => pos; if( pos == -1 ) { w.getVector(expr, wordVector); for( 0 => int i; i < w.dim(); i++ ) { wordVector[i] * multiplier +=> exprVector[i]; } break; } else { expr.substring(0, pos) => word; w.getVector(word, wordVector); for( 0 => int i; i < w.dim(); i++ ) { wordVector[i] * multiplier +=> exprVector[i]; } expr.substring(pos + 1) => expr; if( expr.charAt(0) == '+' ) { 1.0 => multiplier; expr.substring(2) => expr; } else { -1.0 => multiplier; expr.substring(2) => expr; } } } // return string results[k]; w.getSimilar( exprVector, k, results ); return results; } // logical analogy: A to B is as C is to [what this function returns] fun static string[] analogy( Word2Vec @ w, string A, string B, string C, int k ) { // the vector sum B + " - " + A + " + " + C => string v; return eval( w, v, k ); } } Gain g => ADSR env => dac; SinOsc s => g => PRCRev r => blackhole; SinOsc bell => ADSR envBell => JCRev rev => ADSR bellGate => dac; 110 => s.freq; 0.1 => r.gain; 0.8 => g.gain; "A crystal peak mirrors and blackholes" => string back; ["evening","midnight","nighttime","dark","dusk","nightfall","twilight","darkness","gloaming"] @=> string one[]; ["skyline","horizon","midair","firmament","heaven","high","welkin","sky","light"] @=> string two[]; ["at","end","and","to","into that","of the","with","or","into the","as"] @=> string union[]; [0,2,-1,2,-1,2] @=> int structure[]; "" => string t; spork~sound(); spork~bells(); for(0 => int i; i < structure.size(); i++){ if(structure[i] == 0){ env.keyOff(); bellGate.keyOn(); chout <= IO.newline(); chout.flush(); chout <= IO.newline(); chout.flush(); chout <= back; chout.flush(); 1200::ms => now; chout <= IO.newline(); chout.flush(); env.keyOn(); bellGate.keyOff(); chout <= one[Math.random2(0,one.size()-1)] <= " "; chout.flush(); 1200::ms => now; one[Math.random2(0,one.size()-1)] => t; execute(["eval",t,"+",two[Math.random2(0,one.size()-1)],"-","water"]); execute(["vector",t]); for(0 => int j; j < current.size(); j++){ if(maybe) chout <= union[Math.random2(0,union.size()-1)] <= " "; chout.flush();1200::ms => now; chout <= current[j] <= " "; chout.flush(); 600::ms => now; } chout <= IO.newline(); chout.flush(); chout <= two[Math.random2(0,one.size()-1)] <= " "; chout.flush(); 1200::ms => now; two[Math.random2(0,one.size()-1)] => t; execute(["eval","dark","+",t,"-",one[Math.random2(0,one.size()-1)]]); execute(["vector",t]); for(0 => int j; j < current.size(); j++){ if(maybe) chout <= union[Math.random2(0,union.size()-1)] <= " "; chout.flush();1200::ms => now; chout <= current[j] <= " "; chout.flush(); 1200::ms => now; } chout <= IO.newline(); chout.flush(); chout <= IO.newline(); chout.flush(); } if(structure[i] == 2){ two[Math.random2(0,one.size()-1)] => t; chout <= one[Math.random2(0,one.size()-1)] <= " "; chout.flush(); execute(["eval",t,"+",two[Math.random2(0,one.size()-1)],"-","water"]); execute(["vector",t]); for(0 => int j; j < current.size(); j++){ if(maybe) chout <= union[Math.random2(0,union.size()-1)] <= " "; chout.flush();1200::ms => now; chout <= current[j] <= " "; chout.flush(); 1200::ms => now; } chout <= IO.newline(); chout.flush(); chout <= two[Math.random2(0,one.size()-1)] <= " "; chout.flush(); 1200::ms => now; two[Math.random2(0,two.size()-1)] => t; execute(["eval","dark","+",t,"-",one[Math.random2(0,one.size()-1)]]); execute(["vector",t]); for(0 => int j; j < current.size(); j++){ if(maybe) chout <= union[Math.random2(0,union.size()-1)] <= " "; chout.flush();1200::ms => now; chout <= current[j] <= " "; chout.flush(); 1200::ms => now; } chout <= IO.newline(); chout.flush(); env.keyOff(); bellGate.keyOn(); chout <= back; chout.flush(); 1200::ms => now; bellGate.keyOff(); env.keyOn(); chout <= IO.newline(); chout.flush(); chout <= IO.newline(); chout.flush(); } if(structure[i] == -1){ two[Math.random2(0,one.size()-1)] => t; chout <= t <= " "; chout.flush(); execute(["eval",t,"+",two[Math.random2(0,one.size()-1)],"-","water"]); execute(["vector",t]); for(0 => int j; j < current.size(); j++){ if(maybe) chout <= union[Math.random2(0,union.size()-1)] <= " "; chout.flush();1200::ms => now; chout <= current[j] <= " "; chout.flush(); 1200::ms => now; } chout <= IO.newline(); chout.flush(); two[Math.random2(0,one.size()-1)] => t; chout <= t <= " "; chout.flush(); execute(["eval","dark","+",t,"-",one[Math.random2(0,one.size()-1)]]); execute(["vector",t]); for(0 => int j; j < current.size(); j++){ if(maybe) chout <= union[Math.random2(0,union.size()-1)] <= " "; chout.flush();1200::ms => now; chout <= current[j] <= " "; chout.flush(); 1200::ms => now; } chout <= IO.newline(); chout.flush(); two[Math.random2(0,one.size()-1)] => t; execute(["eval","mountain","+",t,"+",one[Math.random2(0,one.size()-1)]]); execute(["vector",t]); for(0 => int j; j < current.size(); j++){ if(maybe) chout <= union[Math.random2(0,union.size()-1)] <= " "; chout.flush();1200::ms => now; chout <= current[j] <= " "; chout.flush(); 1200::ms => now; } chout <= IO.newline(); chout.flush(); chout <= IO.newline(); chout.flush(); } } fun void sound(){ while(samp => now){ s.phase() + r.last() * Math.remap(vector[0],-100,100,0,0.1) => s.phase; } } fun void bells(){ while(true){ Math.remap(vector[1],-100,100,400,800) => bell.freq; envBell.keyOn(); Math.remap(vector[1],-100,100,100,140)::ms => now; envBell.keyOff(); Math.remap(vector[1],-100,100,100,140)::ms => now; } }